package com.example.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.example.model.entity.Service;
import com.example.model.req.AddServiceReq;
import com.example.model.resp.ListAdminServiceResp;
import com.example.model.resp.ListDetailServiceResp;

@Mapper
public interface ServiceMapper {
    @Select({
            "<script>",
            "SELECT id, pic, createAt, serviceName, description FROM tb_service WHERE id IN ",
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "</script>"
    })
    List<Service> getServicesByIds(@Param("ids") List<Integer> ids);

    @Select("select serviceName from tb_service where id = #{id}")
    String getServiceNameById(@Param("id") Integer id);

    @Select("select s.serviceName from tb_service s join con_employee_service ces on ces.serviceId = s.id where ces.userId = #{userId}")
    String getServiceNameByUserId(@Param("userId") Integer userId);

    @Insert("insert into tb_service(pic, serviceName, description, createAt) values(#{pic}, #{serviceName}, #{description}, CURRENT_TIMESTAMP)")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int addService(AddServiceReq addServiceReq);

    @Delete("delete from tb_service where id = #{id}")
    int detelteService(@Param("id") Integer id);

    @Select("select id as serviceId, serviceName from tb_service")
    List<ListAdminServiceResp> listAdminServices();

    @Select("select s.serviceName, s.id, s.pic, s.description, c.category from tb_service s join con_category_service ccs on ccs.serviceId = s.id join tb_category c on c.id = ccs.categoryId limit #{start}, #{pageSize}")
    List<ListDetailServiceResp> listDetailServices(@Param("start") Integer start, @Param("pageSize") Integer pageSize);

    @Select("select count(id) from tb_service")
    Integer totalService();
}
